package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentFacilityMapper;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.LabelInfoMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Currency;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    FeeValueService feeValueService;
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();
        super.saveOrUpdate(apartmentSubmitVo);
        if (id != null) {
            //删除图片列表
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //删除配套
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
            //删除标签
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
            //删除杂费
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
        }
        id=apartmentSubmitVo.getId();
        //插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(id);
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //插入配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<ApartmentFacility> facilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility facility = ApartmentFacility.builder()
                        .apartmentId(id)
                        .facilityId(facilityInfoId)
                        .build();
                facilities.add(facility);
            }
            apartmentFacilityService.saveBatch(facilities);
        }
        //插入标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel>apartmentLabels=new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .apartmentId(id)
                        .labelId(labelId)
                        .build();
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }
        //插入杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue>feeValues=new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(id)
                        .feeValueId(feeValueId)
                        .build();
                feeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(feeValues);
        }
    }

    @Override
    public IPage<ApartmentItemVo> getApartmentAllInfo(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.getApartmentAllInfo(iPage,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentById(Long id) {
        ApartmentDetailVo detailVo=new ApartmentDetailVo();
        //查询info表
        ApartmentInfo apartmentInfo = super.getById(id);
        if (apartmentInfo==null){
            return null;
        }
        //查询GraphVo
        List<GraphVo> graphVoList= graphInfoService.selectGraphInfoByItemId(ItemType.APARTMENT,id);
        //查询LabelInfo
        List<LabelInfo> labelInfoList=labelInfoService.selectLabelInfoByApartmentId(id);
        //查询FacilityInfo
        List<FacilityInfo>facilityInfoList=facilityInfoService.selectFacilityInfoByApartmentId(id);
        //查询FeeValueVo
        List<FeeValueVo>feeValueVoList=feeValueService.selectFeeValueByApartmentId(id);
        BeanUtils.copyProperties(apartmentInfo,detailVo);
        detailVo.setGraphVoList(graphVoList);
        detailVo.setFacilityInfoList(facilityInfoList);
        detailVo.setFeeValueVoList(feeValueVoList);
        detailVo.setLabelInfoList(labelInfoList);
        return detailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        //判断当前公寓是否有房间
       int count= roomInfoMapper.selectCountByApartmentId(id);
       if (count>0){
           throw  new LeaseException(ResultCodeEnum.DELETE_ERROR);
       }
        //删除info
        apartmentInfoMapper.deleteById(id);
        //删除标签
        labelInfoService.deleteLabelByApartmentId(id);
        //删除杂费
        feeValueService.deleteFeeValueByApartmentId(id);
        //删除配套
        facilityInfoService.deleteFacilityInfoByApartmentId(id);
        //删除图片列/表
        graphInfoService.deleteGraphInfoByApartmentId(ItemType.APARTMENT,id);
    }
}




